package gui;

import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;

import application.FieldGuiUpdate;
import application.MainController;
import domain.GameObject;
import exceptions.ErrorListener;
import gui.menu.StartUpMenu;
import gui.screens.RBCanvas;
import gui.screens.ShootingScreen;

/**
 * Class for the midlet.
 * @author Sandra Mayer
 *
 */
public class RabbitBuster extends MIDlet implements FieldGuiUpdate, ErrorListener {

	private RBCanvas m_RBCanvas;
	private Display display;
	private boolean isInit = false;
	
	/**
	 * Constructor.
	 */
	public RabbitBuster() {
		CanvasManager.setMidlet(this);
	}

	/**
	 * Starts the application.
	 */
	public void startApp() {
		if (!isInit) {
			display = Display.getDisplay(this);
			MainController.addErrorListener(this);
			MainController.addFieldUpdateListener(this);
			
			//StartUpMenu
			StartUpMenu menu = null;
			try {
				menu = new StartUpMenu();
			} catch (Exception e) {
				System.out.println("Error");
			}
			display.setCurrent(menu);
			isInit = true;
		}
	}

	/**
	 * Pauses the application.
	 */
	public void pauseApp() {
		MainController.getInstance().pauseUpateIntervall();
	}

	/**
	 * Destroys the application.
	 */
	public void destroyApp(boolean unconditional) {
		MainController.endProgram();
		notifyDestroyed();
	}

	/**
	 * Sets the canvas.
	 * @param canvas the canvas
	 */
	public void setRBCanvas(RBCanvas canvas) {
		m_RBCanvas = canvas;
	}

	/**
	 * Gets the display.
	 * @return the display
	 */
	public Display getDisplay() {
		return display;
	}

	/**
	 * Shows the shooting screen.
	 * @param obj the game object
	 */
	public void loadShootingScreen(GameObject obj) {
		display.setCurrent(new ShootingScreen(obj));
	}

	/**
	 * Updates the game field.
	 */
	public void update(int[] posOfRabbits, int[] posOfPowerUps,
			int[] posOfArrows) {
		if (m_RBCanvas != null)
			m_RBCanvas.update(posOfRabbits, posOfPowerUps, posOfArrows);
	}

	/**
	 * Updates the arrows and their text.
	 */
	public void updateGraphics(int nrOfArrow, int amountOfRabbits) {
		if (m_RBCanvas != null) 
			m_RBCanvas.updateGraphics(nrOfArrow, amountOfRabbits);
		
	}

	/**
	 * Updates the screen with an exception.
	 */
	public void update(Exception e) {
		CanvasManager.showErrorScreen(e);
	}
}